package com.lagou.controller;

import com.lagou.dao.TokenDao;
import com.lagou.dao.UserDao;
import com.lagou.pojo.Token;
import com.lagou.pojo.User;
import com.lagou.service.CodeServiceFeignClient;
import com.lagou.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserDao userDao;

    @Autowired
    private CodeServiceFeignClient codeServiceFeignClient;

    @Autowired
    private TokenDao tokenDao;




    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return name + "," ;
    }

    @RequestMapping("/register/{email}/{password}/{code}")
    public Integer register(@PathVariable String email, @PathVariable String password, @PathVariable String code) {
        Integer validateResult = codeServiceFeignClient.validate(email, code);
        if (validateResult == 0){
            User user = new User();
            user.setEmail(email);
            Example<User> example = Example.of(user);
            Optional<User> userOptional = userDao.findOne(example);
            if (userOptional.isPresent()) {
                return -1;
            }
            user.setPassword(password);
            userDao.save(user);
            return 0;
        }
        return 2;
    }

    @RequestMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable String email) {
        User user = new User();
        user.setEmail(email);
        Example<User> example = Example.of(user);
        Optional<User> userOptional = userDao.findOne(example);
        if (userOptional.isPresent()) {
            return true;
        }
        return false;
    }

    @RequestMapping("/login/{email}/{password}")
    public boolean isRegistered(@PathVariable String email, @PathVariable String password,HttpServletResponse response) {
        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        Example<User> example = Example.of(user);
        Optional<User> userOptional = userDao.findOne(example);
        //校验通过，生成token并入库
        if (userOptional.isPresent()) {
            String loginToken = JwtUtil.createToken(email);
            Token token = new Token();
            token.setEmail(email);
            token.setToken(loginToken);
            tokenDao.save(token);
            Cookie cookie = new Cookie("login_email",email);
            response.addCookie(cookie);
            return true;
        }
        return false;
    }


}
